package oop2.shop;

public class SuperMarket {
	private String name;
	// 会员
	private Vip[] customers = new Vip[100];
	// 计数器,当前的人数
	int count = 0;

	public SuperMarket(String n) {
		name = n;
	}

	public void showInfo() {
		System.out.println("这个商场" + name + "会员信息如下：");
		if (count == 0) {
			System.out.println("没有会员");
			return;
		}
		for (int i = 0; i < count; i++) {
			customers[i].show();
		}
	}

	// 添加新会员
	public boolean add(Vip v) {
		if (count >= customers.length) {
			return false;
		}
		// 检查卡号是否重复
		if (search(v.getNo()) >= 0)
			return false;// 重复
		customers[count++] = v;
		return true;
	}

	// 查找会员在数组中的位置
	public int search(String no) {
		for (int i = 0; i < count; i++) {
			if (customers[i].getNo().equals(no))
				return i;
		}
		return -1;
	}

	public boolean add(String no, String name, String phone, int jifen) {
		if (count >= customers.length) {
			return false;
		}
		// 没有检查卡号是否重复
		customers[count++] = new Vip(no, name, phone, jifen);
		return true;
	}

	//
	public boolean add2(String no, String name, String phone, int jifen) {

		for (int i = 0; i < customers.length; i++) {
			if (customers[i] == null) {
				customers[i] = new Vip(no, name, phone, jifen);
				return true;
			}
		}

		return false;
	}

	// 涨积分
	public boolean addScore(String no, int newScore) {
		for (int i = 0; i < count; i++) {
			if (customers[i].getNo().equals(no)) {
				customers[i].setScore(customers[i].getScore() + newScore);
				return true;
			}
		}
		return false;
	}

	// 兑换
	public String exchange(String no, int type) {
		int pos = search(no);// 找到他的位置
		if (pos < 0) {
			System.out.println("没有这个会员");
			return null;
		}
		Vip v = customers[pos];// 找到这个会员
		switch (type) {
		case 1: {
			if (v.getScore() < 100) {
				return null;
			}
			v.setScore(v.getScore() - 100);
			return "餐巾纸一包";
		}
		case 2: {
			if (v.getScore() < 400) {
				return null;
			}
			v.setScore(v.getScore() - 400);
			return "剃须刀一个";
		}
		case 3: {
			if (v.getScore() < 1000) {
				return null;
			}
			v.setScore(v.getScore() - 1000);
			return "炒锅一个";
		}
		}
		return null;
	}
}
